StoreRoomView = Ext.extend(Ext.Panel, {
	constructor : function(a) {
		Ext.applyIf(this, a);
		this.initUIComponents();
		StoreRoomView.superclass.constructor.call(this, {
			id : "StoreRoomView",
			title : "库房管理",
			iconCls : "menu-storeRoom",
			layout : "border",
			autoScroll : true
		});
	},
	initUIComponents : function() {
		this.initSearchPanel();
		this.initGridPanel();
		this.items = [ this.searchPanel, this.gridPanel ];
	},
	onSearch : function(c) {
		var a = Ext.getCmp("StoreRoomSearchForm");
		var b = Ext.getCmp("StoreRoomGrid");
		if (a.getForm().isValid()) {
			$search({
				searchPanel : a,
				gridPanel : b
			});
		}
	}
});
StoreRoomView.prototype.initSearchPanel = function() {
	this.searchPanel = new Ext.FormPanel({
		region : "north",
		height : 35,
		frame : false,
		border : false,
		id : "StoreRoomSearchForm",
		layout : "hbox",
		layoutConfig : {
			padding : "5",
			align : "middle"
		},
		defaults : {
			xtype : "label",
			border : false,
			margins : {
				top : 0,
				right : 4,
				bottom : 4,
				left : 4
			}
		},
		items : [{
			text : "库房名称"
		}, {
			xtype : "textfield",
			name : "Q_storeRoomName_S_LK"
		}, {
			text : "库房类别"
		},{
			xtype : "combo",
			fieldLabel: '库房类别', 
			editable : false,
			allowBlank : true,
			width:150,
			id : "storeRoomType.detailId",
			hiddenName : "Q_storeRoomType.detailId_L_EQ",
			name : "storeRoomType.codeName",
			mode : "local",
			lazyInit : false,
			forceSelection : false,
			blankText : '请选择库房类别', 
			emptyText : '请选择库房类别', 			
			triggerAction : "all",
			store : new Ext.data.JsonStore({
				url : __ctxPath + "/system/listDicDetail.do?Q_dicType.dic_S_EQ=库房",
				autoLoad : true,
				autoShow : true,
				root : "result",
				idProperty : "detailId",
				fields : [ "detailId", "codeName" ]
			}),
			valueField : "detailId",
			displayField : "codeName"			
		},{
			xtype : "button",
			text : "查询",
			iconCls : "search",
			scope : this,
			handler : this.onSearch.createCallback(this)
		},{
			xtype : "button",
			text : "清空",
			scope : this,
			iconCls : "reset",
			handler : StoreRoomView.reset
		} ]
	});
};
StoreRoomView.prototype.initGridPanel = function() {
	this.toolbar = new Ext.Toolbar({
		height : 30,
		items : []
	});
	if (isGranted("_StoreRoomAdd")) {
		this.toolbar.add(new Ext.Button({
			text : "添加库房",
			iconCls : "btn-add",
			handler : function() {
				
				
				new StoreRoomForm().show();
//				var f = Ext.getCmp("centerTabPanel");
//				var g = Ext.getCmp("StoreRoomForm");
//				if (g == null) {
//					g = new StoreRoomForm("增加库房");
//					f.add(g);
//				} else {
//					f.remove(g);
//					g = new StoreRoomForm("增加库房");
//					f.add(g);
//				}
//				f.activate(g);
			}
		}));
	}
	if (isGranted("_StoreRoomDel")) {
		this.toolbar.add(new Ext.Button({
			iconCls : "btn-delete",
			text : "删除库房",
			handler : function() {
				var h = Ext.getCmp("StoreRoomGrid");
				var f = h.getSelectionModel().getSelections();
				if (f.length == 0) {
					Ext.ux.Toast.msg("信息", "请选择要删除的记录！");
					return;
				}
				var j = Array();
				
				for ( var g = 0; g < f.length; g++) {
					if (f[g].data.srId != 0) {
						j.push(f[g].data.id);
					} 
				}
				
					StoreRoomView.remove(j);
				 
			}
		}));
	}
	var b = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : __ctxPath + "/system/listStoreRoom.do"
		}),
		reader : new Ext.data.JsonReader({
			root : "result",
			totalProperty : "totalCounts",
			fields : [ 
				 "id", "storeRoomName", "storeRoomType", "remark"]
		}),
		remoteSort : true
	});
	b.setDefaultSort("storeRoomType.detailId", "desc");
	b.load({
		params : {
			start : 0,
			limit : 25
		}
	});
	var e = new Ext.grid.CheckboxSelectionModel();
	var a = new Ext.grid.ColumnModel(
			{
				columns : [
						e,
						new Ext.grid.RowNumberer(),
						{
							header : "id",
							dataIndex : "id",
							hidden : true
						},
						
						{
							header : "库房名称",
							dataIndex : "storeRoomName"
						},
						{
							header : "库房类别",
							dataIndex : "storeRoomType",							
							renderer : function(f) {
								if (f == null) {
									return "";
								} else {
									return f.codeName;
								}
							},
							width : 60
						},
						{
							header : "备注",
							dataIndex : "remark",
							width : 120
						},
						
						
						{
							header : "管理",
							dataIndex : "id",
							sortable : false,
							width : 60,
							renderer : function(j, i, g, m, h) {
								var l = g.data.id;
								var f = g.data.storeRoomName;
								var k = "";
								if (l != 0) {
									if (isGranted("_StoreRoomDel")) {
										k += '<button title="删除" value=" " class="btn-delete" onclick="StoreRoomView.remove('
												+ l + ')"></button>';
									}
									if (isGranted("_StoreRoomEdit")) {
										k += '&nbsp;<button title="编辑" value=" " class="btn-edit" onclick="StoreRoomView.edit('
											+ l + ')"></button>';
									}
								}
								return k;
							}
						} ],
				defaults : {
					sortable : true,
					menuDisabled : true,
					width : 100
				}
			});
	this.gridPanel = new Ext.grid.GridPanel({
		id : "StoreRoomGrid",
		tbar : this.toolbar,
		store : b,
		region : "center",
		shim : true,
		trackMouseOver : true,
		disableSelection : false,
		loadMask : true,
		cm : a,
		sm : e,
		viewConfig : {
			forceFit : true,
			enableRowBody : false,
			showPreview : false
		},
		bbar : new HT.PagingBar({
			store : b
		})
	});
//	this.gridPanel.addListener("rowdblclick", d);
	var c = this.gridPanel;
	function d(g, f, h) {
		g.getSelectionModel().each(function(j) {
			var i = j.data.srId;
			if (isGranted("_StoreRoomEdit") && i != 1) {
				StoreRoomView.edit(i);
			}
		});
	}
};
StoreRoomView.remove = function(b) {
	var agrid = Ext.getCmp("StoreRoomGrid");
	Ext.Msg.confirm("信息确认", "您确认要删除该记录吗？", function(c) {
		if (c == "yes") {
			Ext.Ajax.request({
				url : __ctxPath + "/system/deleteStoreRoom.do",
				params : {
					ids : b
				},
				method : "post",
				success : function(a) {
					var result=Ext.util.JSON.decode(a.responseText); 
					if(result.success){
						Ext.ux.Toast.msg("信息", "成功删除所选记录！");
						agrid.getStore().reload({
							params : {
								start : 0,
								limit : 25
							}
						});
					}else{ 
						Ext.MessageBox.show({
							title : "操作提示",
							msg : result.msg,
							buttons : Ext.MessageBox.OK,
							icon : "ext-mb-error"
						});
						agrid.getStore().reload({
							params : {
								start : 0,
								limit : 25
							}
						});
					}
				}
			});
			
		}
		
		
	});
};
StoreRoomView.reset = function(a) {
	var a = Ext.getCmp("StoreRoomSearchForm");
	a.getForm().reset();
};
StoreRoomView.edit = function(a) {
	new StoreRoomForm({
		id : a
	}).show();

};